package com.gorkr.hot100.simple;

import org.junit.jupiter.api.Test;

import java.util.LinkedList;

/**
 * @author gorkr
 * @date 2022/03/10 20:51
 * 有效的括号
 *
 * 思路： 用栈存
 **/
public class L20 {

    public boolean isValid(String s) {
        LinkedList<Character> stack = new LinkedList<>();
        char[] chars = s.toCharArray();
        try {
            for (char c: chars
            ) {
                if(c=='('||c=='['||c=='{'){
                    stack.push(c);
                }else {
                    if(c==')'){
                        if(stack.pop()!='('){
                            return false;
                        }
                    }else if(c==']'){
                        if(stack.pop()!='['){
                            return false;
                        }
                    }else {
                        if(stack.pop()!='{'){
                            return false;
                        }
                    }
                }

            }


        }catch (Exception e){
            return false;
        }
        if(!stack.isEmpty()){
            return false;
        }

        return true;
    }

    @Test
    public void test(){
        String s = "(";
        System.out.println(isValid(s));

    }
}
    